/**
 * 字典管理commonDict.js
 * 版本号： v1.0.0
 */
$(function () {
    //绑定模块名称
    bindCombo_modelDist("modelName_");

    //字典模块绑定功能名称
    bindCombo_funDist("funName_");

    //字典函数绑定属性名称
    bindCombo_propertyDist("propertyName_");


    //设置输入框的最大输入长度
    $(".easyui-textbox").each(function(){
        var data_options = $(this).attr("data-options");
        if(data_options){
            var matchs = data_options.match(/length\[1,(\d*)\]/);
            if(matchs){
                $(this).textbox("textbox").attr("maxlength", matchs[1] - 0 + 1);
            }else{
                $(this).textbox("textbox").attr("maxlength", 20);
            }
        }else{
            $(this).textbox("textbox").attr("maxlength", 20);
        }
    });


    /**
     * 字典列表
     */
    var lastChecked=0;
     $("#commonDict_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        striped: true,
        pageSize: 10,
        pageNumber: 1,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/commonDict/commonDictList.do',
        idField: 'cdId',
        frozenColumns: [[
            {field: 'cdId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'modelName', title: '模块名称', width: 90, fixed: true},
                {field: 'modelCode', title: '模块代码', width: 90, fixed: true},
                {field: 'funName', title: '功能名称', width: 120, fixed: true},
                {field: 'funCode', title: '功能代码', width: 120, fixed: true},
                {field: 'propertyName', title: '属性名称', width: 140, fixed: true},
                {field: 'propertyCode', title: '属性代码', width: 100, fixed: true},
                {field: 'status', title: '是否启用', width: 60, fixed: true, align: 'center', fixed: true,
                    formatter: function (val) {
                        if (val == 1) return "<span style='color: green; font-weight: bold'>启用</span>";
                        if (val == 2) return "<span style='color: red; font-weight: bold'>关闭</span>";
                    }
                },
                {field: 'createDate', title: '创建时间', width: 135}
            ]
        ],
         onCheck:function(rowIndex, rowData){
             var row = $("#commonDict_dataList").datagrid("getChecked");
             if(row && rowIndex == lastChecked){
                 $('#commonDict_dataList').datagrid("unselectAll");//取消选中当前所有行
                 lastChecked = null;
             }else{
                 lastChecked = rowIndex;
             }
         },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#commonDict_dataList").datagrid("selectRow", 0);
            }
        }
    });


    //定义可操作按钮
    $("#commonDict_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });


});



/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                bindCombo_modelDist("modelName_");
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            commonDict_loadSearch();
                        }
                    });
                }
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                                id:btnOptions[i].value.meId,
                                text:btnOptions[i].value.name,
                                iconCls:btnOptions[i].value.iconCls,
                                handler: function () {
                                    window.location.href = CONTEXT_PATH + "/commonDict/toCommonDict.do";
                                }
                    });
                }
                //修改按钮
                if("editBtn" == btnOptions[i].key){
                    toolbar.push({
                                id:btnOptions[i].value.meId,
                                text:btnOptions[i].value.name,
                                iconCls:btnOptions[i].value.iconCls,
                                handler: function () {
                                    commonDict_edit();
                                }
                    });
                }
                //删除按钮
                if("delBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            commonDict_del();
                        }
                    });
                }
                //查看详情按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                                id:btnOptions[i].value.meId,
                                text:btnOptions[i].value.name,
                                iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            commonDict_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 查看字典
 */
function commonDict_view(iconCls){
    var row = $("#commonDict_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的字典详情", timeout: 3000, showType: "slide" });
        return;
    }
    var row = $("#commonDict_dataList").datagrid("getSelected");
    //IFRAM打开窗口
    layoutSub(zdxq.id, zdxq.url + row.cdId, iconCls, zdxq.closable, zdxq.title);
}



/**
 * 删除字典配置
 */
function commonDict_del() {
    var checked = $("#commonDict_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择要删除的字典配置", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#commonDict_dataList").datagrid("getSelected");
    $.messager.confirm('删除提示', '您确定要删除 ' + row.propertyName + ' 字典配置吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/commonDict/del.do?cdId=" + row.cdId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#commonDict_dataList").datagrid("reload", {
                        modelCode: $("#modelName_").combobox("getValue"),
                        funCode: $("#funName_").combobox("getValue"),
                        propertyCode: $("#propertyName_").combobox("getValue")
                    });
                }
            })
        }
    });
}


/**
 * 修改字典配置
 * @returns {boolean}
 */
function commonDict_edit() {
    var checked = $("#commonDict_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择要修改的字典配置", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#commonDict_dataList").datagrid("getSelected");
    window.location.href = CONTEXT_PATH + "/commonDict/toCommonDict.do?cdId=" + row.cdId;
}


/**
 *  加载查询
 */
function commonDict_loadSearch() {
    //刷新dg页码
    refreshPageNumber("commonDict_dataList");
    //查询数据
    $("#commonDict_dataList").datagrid("reload", {
        modelCode: $("#modelName_").combobox("getValue"),
        funCode: $("#funName_").combobox("getValue"),
        propertyCode: $("#propertyName_").combobox("getValue")
    });
}

/**
 * 字典模块函数bind事件
 * @param modelControl
 */
function bindCombo_modelDist(modelControl) {
    $.ajax({
        url: CONTEXT_PATH + "/commonDict/getModelList.do",
        type: "get",
        success: function (data) {
            var themecombo = [{'text': '--请选择--', 'value': ''}];
            for (var i = 0; i < data.length; i++) {
                themecombo.push({"text": data[i].modelName, "value": data[i].modelCode});
            }
            $("#" + modelControl).combobox("loadData", themecombo);
        }
    })
}

/**
 * 字典功能函数onChange事件
 * @param funControl
 */
function bindCombo_funDist(funControl) {
    $("#modelName_").combobox({
        onChange: function (n, o) {
            $("#funName_").combobox("setValue", ''); //清空
            $("#propertyName_").combobox("setValue", ''); //清空
            clear:true;
            $.ajax({
                url: CONTEXT_PATH + "/commonDict/getFunList.do",
                data: {modelCode: n},
                type: "get",
                success: function (data) {
                    var themecombo = [{'text': '--请选择--', 'value': ''}];
                    for (var i = 0; i < data.length; i++) {
                        themecombo.push({"text": data[i].funName, "value": data[i].funCode});
                    }
                    $("#" + funControl).combobox("loadData", themecombo);
                }
            })
        }
    });
}


/**
 * 字典属性函数onChange事件
 * @param propertyControl 字典属性控件Id
 * @private
 */
function bindCombo_propertyDist(propertyControl) {
    $("#funName_").combobox({
        onChange: function (n, o) {
            $("#propertyName_").combobox("setValue", ''); //清空
            $.ajax({
                url: CONTEXT_PATH + "/commonDict/getPropertyList.do",
                data: {funCode: n},
                type: "get",
                success: function (data) {
                    var themecombo = [{'text': '--请选择--', 'value': ''}];
                    for (var i = 0; i < data.length; i++) {
                        themecombo.push({"text": data[i].propertyName, "value": data[i].propertyCode});
                    }
                    $("#" + propertyControl).combobox("loadData", themecombo);
                }
            })
        }
    });
}








